package com.yic.module.device.controller.admin.maintenanceDeviceEntry;

import com.yic.module.device.dal.dataobject.maintenancePlanEntry.MaintenancePlanEntryDO;
import com.yic.module.device.service.maintenancePlanEntry.MaintenancePlanEntryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.maintenanceDeviceEntry.vo.*;
import com.yic.module.device.dal.dataobject.maintenanceDeviceEntry.MaintenanceDeviceEntryDO;
import com.yic.module.device.convert.maintenanceDeviceEntry.MaintenanceDeviceEntryConvert;
import com.yic.module.device.service.maintenanceDeviceEntry.MaintenanceDeviceEntryService;

@Tag(name = "管理后台 - 设备与保养项关联")
@RestController
@RequestMapping("/device/maintenance-device-entry")
@Validated
public class MaintenanceDeviceEntryController {

    @Resource
    private MaintenanceDeviceEntryService maintenanceDeviceEntryService;
    @Resource
    private MaintenancePlanEntryService maintenancePlanEntryService;

    @PostMapping("/create")
    @Operation(summary = "创建设备与保养项关联")
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:create')")
    public CommonResult<Long> createMaintenanceDeviceEntry(@Valid @RequestBody MaintenanceDeviceEntryCreateReqVO createReqVO) {
        return success(maintenanceDeviceEntryService.createMaintenanceDeviceEntry(createReqVO));
    }

    @PostMapping("/createBatch")
    @Operation(summary = "批量创建设备与保养项关联")
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:create')")
    public CommonResult<Void> createBatchMaintenanceDeviceEntry(@Valid @RequestBody List<MaintenanceDeviceEntryCreateReqVO> createReqVO) {
        return success(maintenanceDeviceEntryService.createBatchMaintenanceDeviceEntry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备与保养项关联")
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:update')")
    public CommonResult<Boolean> updateMaintenanceDeviceEntry(@Valid @RequestBody MaintenanceDeviceEntryUpdateReqVO updateReqVO) {
        maintenanceDeviceEntryService.updateMaintenanceDeviceEntry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备与保养项关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:delete')")
    public CommonResult<Boolean> deleteMaintenanceDeviceEntry(@RequestParam("id") Long id) {
        maintenanceDeviceEntryService.deleteMaintenanceDeviceEntry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备与保养项关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:query')")
    public CommonResult<MaintenanceDeviceEntryRespVO> getMaintenanceDeviceEntry(@RequestParam("id") Long id) {
        MaintenanceDeviceEntryDO maintenanceDeviceEntry = maintenanceDeviceEntryService.getMaintenanceDeviceEntry(id);
        return success(MaintenanceDeviceEntryConvert.INSTANCE.convert(maintenanceDeviceEntry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备与保养项关联列表")
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:query')")
    public CommonResult<List<MaintenanceDeviceEntryRespVO>> getMaintenanceDeviceEntryList(@Valid MaintenanceDeviceEntryExportReqVO exportReqVO) {
        List<MaintenanceDeviceEntryDO> list = maintenanceDeviceEntryService.getMaintenanceDeviceEntryList(exportReqVO);
        return success(MaintenanceDeviceEntryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备与保养项关联分页")
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:query')")
    public CommonResult<PageResult<MaintenanceDeviceEntryRespVO>> getMaintenanceDeviceEntryPage(@Valid MaintenanceDeviceEntryPageReqVO pageVO) {
        PageResult<MaintenanceDeviceEntryDO> pageResult = maintenanceDeviceEntryService.getMaintenanceDeviceEntryPage(pageVO);
        PageResult<MaintenanceDeviceEntryRespVO> result = MaintenanceDeviceEntryConvert.INSTANCE.convertPage(pageResult);
        result.getList().forEach(item->{
            MaintenancePlanEntryDO maintenancePlanEntry = maintenancePlanEntryService.getMaintenancePlanEntry(item.getEntryId());
            item.setEntryId(maintenancePlanEntry.getId());
            item.setName(maintenancePlanEntry.getName());
            item.setContent(maintenancePlanEntry.getContent());
            item.setStatus(maintenancePlanEntry.getStatus());
            item.setRemark(maintenancePlanEntry.getRemark());
            item.setWorkHours(maintenancePlanEntry.getWorkHours());
            item.setType(maintenancePlanEntry.getType());
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备与保养项关联 Excel")
    @PreAuthorize("@ss.hasPermission('device:maintenance-device-entry:export')")
    @OperateLog(type = EXPORT)
    public void exportMaintenanceDeviceEntryExcel(@Valid MaintenanceDeviceEntryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MaintenanceDeviceEntryDO> list = maintenanceDeviceEntryService.getMaintenanceDeviceEntryList(exportReqVO);
        // 导出 Excel
        List<MaintenanceDeviceEntryExcelVO> datas = MaintenanceDeviceEntryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备与保养项关联.xls", "数据", MaintenanceDeviceEntryExcelVO.class, datas);
    }

}
